package com.mjfeed.controller.shop;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.mjfeed.controller.BaseController;
import com.mjfeed.entity.ForumReply;
import com.mjfeed.entity.ForumTopic;
import com.mjfeed.entity.Member;
import com.mjfeed.entity.ProductCategory;
import com.mjfeed.service.ForumReplyService;
import com.mjfeed.service.ForumTopicService;
import com.mjfeed.service.IndexImageService;
import com.mjfeed.service.MemberService;
import com.mjfeed.service.ProductCategoryService;
import com.mjfeed.util.Format;
import com.mjfeed.util.Pagination;
import com.mjfeed.util.ParamBean;

/**
 * @description 论坛话题
 * @author zhaominglei
 * @date 2015年1月9日
 */
@Controller("shopForumTopicController")
public class ForumTopicController extends BaseController {
	@Resource(name="productCategoryService")
	private ProductCategoryService productCategoryService;
    @Resource(name="forumTopicService")
    private ForumTopicService forumTopicService;
    @Resource(name="memberService")
	private MemberService memberService;
    @Resource(name="forumReplyService")
    private ForumReplyService forumReplyService;
    @Resource(name="indexImageService")
    private IndexImageService indexImageService;
    
    @RequestMapping(value = "/forumTopic/list", method=RequestMethod.GET)
	public String list(ParamBean paramBean, HttpServletRequest request, HttpServletResponse response, Model model){
    	Member member = memberService.getCurrent();
    	paramBean.setUrl(getBasePath(request) + "/forumTopic/list.shtml?productCategoryId="+paramBean.getProductCategoryId());
		int p = (Format.null2Zero(request.getParameter("p")) == 0) ? 1 : Format.null2Zero(request.getParameter("p"));
		paramBean.setPage(p);
		Pagination<ForumTopic> pagination = forumTopicService.getPagination(paramBean);
		request.setAttribute("pagination", pagination);
		request.setAttribute("paramBean", paramBean);
		request.setAttribute("productCategoryId", paramBean.getProductCategoryId());
		request.setAttribute("productCategoryTree", productCategoryService.findRoots());
		request.setAttribute("member", member);
		request.setAttribute("indexImages", indexImageService.getAllIndexImage());
		request.setAttribute("todayCount", forumTopicService.getTodayCount());
		request.setAttribute("yesterdayCount", forumTopicService.getYesterdayCount());
		request.setAttribute("count", forumTopicService.getCount());
		request.setAttribute("memberCount", memberService.getCount());
		request.setAttribute("lastMember", memberService.getLastMember());
		return "shop/forumTopic/list";
	}
	
	@RequestMapping(value = "/member/forumTopic/add",method=RequestMethod.GET)
	public String add(HttpServletRequest request, HttpServletResponse response, Model model){
		Member member = memberService.getCurrent();
		if (member == null) {
			return ERROR_VIEW;
		}
		List<ProductCategory> productCategories = productCategoryService.findRoots();
		request.setAttribute("productCategoryTree", productCategories);
		request.setAttribute("productCategoryId", request.getParameter("productCategoryId"));
		return "shop/forumTopic/add";
	}
	
	@RequestMapping(value = "/member/forumTopic/save", method=RequestMethod.POST)
	public String save(ForumTopic forumTopic, HttpServletRequest request, HttpServletResponse response, Model model){
		Member member = memberService.getCurrent();
		if (member == null) {
			return ERROR_VIEW;
		}
		String productCategoryId = request.getParameter("productCategoryId");
		if (productCategoryId == null || productCategoryId.equals("")) {
			return ERROR_VIEW;
		}
		ProductCategory productCategory = productCategoryService.getById(ProductCategory.class, Long.valueOf(productCategoryId));
		if (productCategory == null) {
			return ERROR_VIEW;
		}
		forumTopic.setType(ForumTopic.FORUM_TOPIC_TYPE);
		forumTopic.setProductCategory(productCategory);
		forumTopic.setMember(member);
		forumTopic.setIp(super.getRemoteIpAddr(request));
		forumTopicService.save(forumTopic);
		return "redirect:/forumTopic/list.shtml?productCategoryId="+productCategoryId;
	}
	
	@RequestMapping(value = "/member/forumTopic/edit",method=RequestMethod.GET)
	public String edit(Long id, HttpServletRequest request, HttpServletResponse response, Model model){
		Member member = memberService.getCurrent();
		if (member == null) {
			return ERROR_VIEW;
		}
		if (id == null || id.longValue() < 1) {
			return ERROR_VIEW;
		}
		List<ProductCategory> productCategories = productCategoryService.findRoots();
		request.setAttribute("productCategoryTree", productCategories);
		request.setAttribute("forumTopic", forumTopicService.getById(ForumTopic.class, id));
		return "shop/forumTopic/edit";
	}
	
	@RequestMapping(value = "/member/forumTopic/update", method=RequestMethod.POST)
	public String update(ForumTopic forumTopic, HttpServletRequest request, HttpServletResponse response, Model model){
		Member member = memberService.getCurrent();
		if (member == null) {
			return ERROR_VIEW;
		}
		String productCategoryId = request.getParameter("productCategoryId");
		if (productCategoryId == null || productCategoryId.equals("")) {
			return ERROR_VIEW;
		}
		ProductCategory productCategory = productCategoryService.getById(ProductCategory.class, Long.valueOf(productCategoryId));
		if (productCategory == null) {
			return ERROR_VIEW;
		}
		forumTopic.setType(ForumTopic.FORUM_TOPIC_TYPE);
		forumTopic.setProductCategory(productCategory);
		forumTopic.setMember(member);
		forumTopic.setIp(super.getRemoteIpAddr(request));
		forumTopicService.update(forumTopic);
		return "redirect:/forumTopic/list.shtml?productCategoryId="+productCategoryId;
	}
	
	@RequestMapping(value = "/forumTopic/view",method=RequestMethod.GET)
	public String view(Long id, ParamBean paramBean, HttpServletRequest request, HttpServletResponse response, Model model){
		if (id == null || id.longValue() < 1) {
			return ERROR_VIEW;
		}
		ForumTopic forumTopic = forumTopicService.getById(ForumTopic.class, id);
		if (forumTopic == null) {
			return ERROR_VIEW;
		}
		int viewCount = forumTopic.getViewCount() + 1;
		forumTopic.setViewCount(viewCount);
		forumTopicService.update(forumTopic);
		paramBean.setUrl(getBasePath(request) + "forumTopic/view.shtml?id="+id);
		int p = (Format.null2Zero(request.getParameter("p")) == 0) ? 1 : Format.null2Zero(request.getParameter("p"));
		paramBean.setPage(p);
		paramBean.setProductCategoryId(forumTopic.getProductCategory().getId());
		paramBean.setForumTopicId(forumTopic.getId());
		Pagination<ForumReply> pagination = forumReplyService.getPagination(paramBean);
		request.setAttribute("pagination", pagination);
		request.setAttribute("productCategoryTree", productCategoryService.findRoots());
		request.setAttribute("forumTopic", forumTopic);
		request.setAttribute("member", memberService.getCurrent());
		request.setAttribute("indexImages", indexImageService.getAllIndexImage());
		return "shop/forumTopic/view";
	}
	
	@RequestMapping(value = "/member/forumTopic/delete",method=RequestMethod.GET)
	public String delete(Long id, HttpServletRequest request, HttpServletResponse response, Model model){
		Member member = memberService.getCurrent();
		if (member == null) {
			return ERROR_VIEW;
		}
		if (id == null || id.longValue() < 1) {
			return ERROR_VIEW;
		}
		ForumTopic forumTopic = forumTopicService.getById(ForumTopic.class, id);
		if (forumTopic == null) {
			return ERROR_VIEW;
		}
		Long productCategoryId = forumTopic.getProductCategory().getId();
		forumTopicService.delete(forumTopic);
		forumReplyService.deleteAll(forumTopic);
		return "redirect:/forumTopic/list.shtml?productCategoryId="+productCategoryId;
	}
	
    @RequestMapping(value = "/collegeTopic/list", method=RequestMethod.GET)
	public String collegeList(ParamBean paramBean, HttpServletRequest request, HttpServletResponse response, Model model){
    	Member member = memberService.getCurrent();
    	paramBean.setUrl(getBasePath(request) + "/collegeTopic/list.shtml?productCategoryId="+paramBean.getProductCategoryId());
    	paramBean.setForumType(ForumTopic.COLLEGE_TOPIC_TYPE);
		int p = (Format.null2Zero(request.getParameter("p")) == 0) ? 1 : Format.null2Zero(request.getParameter("p"));
		paramBean.setPage(p);
		Pagination<ForumTopic> pagination = forumTopicService.getPagination(paramBean);
		request.setAttribute("pagination", pagination);
		request.setAttribute("paramBean", paramBean);
		request.setAttribute("productCategoryId", paramBean.getProductCategoryId());
		request.setAttribute("productCategoryTree", productCategoryService.findRoots());
		request.setAttribute("member", member);
		request.setAttribute("indexImages", indexImageService.getAllIndexImage());
		return "shop/collegeTopic/list";
	}
    
    @RequestMapping(value = "/collegeTopic/view",method=RequestMethod.GET)
	public String collegeView(Long id, ParamBean paramBean, HttpServletRequest request, HttpServletResponse response, Model model){
		if (id == null || id.longValue() < 1) {
			return ERROR_VIEW;
		}
		ForumTopic forumTopic = forumTopicService.getById(ForumTopic.class, id);
		if (forumTopic == null) {
			return ERROR_VIEW;
		}
		paramBean.setUrl(getBasePath(request) + "collegeTopic/view.shtml?id="+id);
		int p = (Format.null2Zero(request.getParameter("p")) == 0) ? 1 : Format.null2Zero(request.getParameter("p"));
		paramBean.setPage(p);
		paramBean.setProductCategoryId(forumTopic.getProductCategory().getId());
		paramBean.setForumTopicId(forumTopic.getId());
		Pagination<ForumReply> pagination = forumReplyService.getPagination(paramBean);
		request.setAttribute("pagination", pagination);
		request.setAttribute("productCategoryTree", productCategoryService.findRoots());
		request.setAttribute("collegeTopic", forumTopic);
		request.setAttribute("member", memberService.getCurrent());
		request.setAttribute("indexImages", indexImageService.getAllIndexImage());
		return "shop/collegeTopic/view";
	}
}